package com.kuang.user.controller;


import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import com.kuang.user.service.OrderService;
import com.kuang.user.service.impl.OrderServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author wxy
 * @since 2023-07-04
 */
@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    // TODO: 2023/7/4 导出十万数据
    @GetMapping("orderPage")
    public String orderList(HttpServletResponse response) {
        for (int i = 1; i < 11; i++) {
            //第一页  固定一万
            this.orderService.findPage(response, 10000, i);
        }
        return "ok";
    }


    // TODO: 2023/7/4 批量上传视频
    @PostMapping("uploadMP4")
    public String uploadMP4(@RequestParam MultipartFile[] files) throws IOException {
        /*遍历每一个视频文件*/
        for (MultipartFile file : files) {
            this.orderService.uploadMP4(file);
        }
        return "ok";
    }


    /**
     * 多线程生成excel压缩zip到浏览器下载
     *
     * @param zipPath            自定义本地存放zip路径
     * @param excelDirectoryPath 自定义本地存放excel目录路径
     * @param response           响应
     * @return {@link String}
     */
    @GetMapping("addExcel")
    public String addExcel(String zipPath, String excelDirectoryPath, HttpServletResponse response) {

        try {

            Boolean aBoolean = this.orderService.addExcel(zipPath, excelDirectoryPath, response);

            return "ok";
        } catch (Exception e) {
            e.printStackTrace();
            return e.getMessage();
        }
    }


    /**
     * 通用文件下载方法
     *
     * @param path     本地文件存放路径
     * @param response 响应
     * @return {@link HttpServletResponse}
     */
    @GetMapping("/download")
    public HttpServletResponse download(String path, HttpServletResponse response) {

        try {
            // path是指欲下载的文件的路径。
            File file = new File(path);
            // 取得文件名。
            String filename = file.getName();
            // 取得文件的后缀名。
            String ext = filename.substring(filename.lastIndexOf(".") + 1).toUpperCase();
            // 以流的形式下载文件。
            InputStream fis = new BufferedInputStream(new FileInputStream(path));
            byte[] buffer = new byte[fis.available()];
            fis.read(buffer);
            fis.close();
            // 清空response
            response.reset();
            // 设置response的Header
            response.addHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes()));
            response.addHeader("Content-Length", "" + file.length());
            OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
            response.setContentType("application/octet-stream");
            toClient.write(buffer);
            toClient.flush();
            toClient.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return response;
    }

}

